<template>
  <div class="sidebar">
    <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="sidebar.collapse"
        background-color="#324157"
        text-color="#bfcbd9"
        active-text-color="#20a0ff"
        unique-opened
        router
    >
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-sub-menu :index="item.index" :key="item.index" v-permiss="item.permiss">
            <template #title>
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-sub-menu
                  v-if="subItem.subs"
                  :index="subItem.index"
                  :key="subItem.index"
                  v-permiss="item.permiss"
              >
                <template #title>{{ subItem.title }}</template>
                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">
                  {{ threeItem.title }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.index" v-permiss="item.permiss">
                {{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.index" :key="item.index" v-permiss="item.permiss">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.title }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
import {computed} from 'vue';
import {useSidebarStore} from '../store/sidebar';
import {useRoute} from 'vue-router';

const items = [
  {
    icon: 'Odometer',
    index: '/dashboard',
    title: '系统首页',
    permiss: '12',
  },
  {
    icon: 'Calendar',
    index: '1',
    title: '资料管理',
    permiss: '1',
    subs: [
      {
        index: '/supplier',
        title: '供应商管理',
        permiss: '1',
      },
      {
        index: '/customer',
        title: '客户管理',
        permiss: '2',
      },
    ],
  },
  {
    icon: 'DocumentCopy',
    index: '/tabs',
    title: '采购管理',
    permiss: '2',
    subs: [
      {
        index: '/purchase',
        title: '采购入库',
        permiss: '3',
      },
      {
        index: '/purchaseMes',
        title: '采购信息查询',
        permiss: '4',
      },
    ]
  },
  {
    icon: 'Edit',
    index: '3',
    title: '销售入库',
    permiss: '3',
    subs: [
      {
        index: '/sale',
        title: '销售出库',
        permiss: '5',
      },
      {
        index: '/saleMes',
        title: '销售信息查询',
        permiss: '6',
      },
    ],
  },
  {
    icon: 'Setting',
    index: '/icon',
    title: '库存管理',
    permiss: '4',
    subs:[
      {
        index: '/stock',
        title: '库存一览',
        permiss: '7',
      },
    ]
  },
  {
    icon: 'PieChart',
    index: '/charts',
    title: '统计信息',
    permiss: '5',
    subs:[
      {
        index: '/saleStatistics',
        title: '销售统计',
        permiss: '8',
      },
      {
        index: '/saleStatisticsMonth',
        title: '月销售统计',
        permiss: '9',
      },
    ]
  },
  {
    icon: 'DocumentCopy',
    index: '/permission',
    title: '权限管理',
    permiss: '6',
    subs:[
      {
        index: '/user',
        title: '用户管理',
        permiss: '10',
      },
      {
        index: '/role',
        title: '角色管理',
        permiss: '11',
      },
    ]
  },
  {
    icon: 'Setting',
    index: '/donate',
    title: '个人信息管理',
    permiss: '12',
    subs:[
      {
        index: '/password',
        title: '修改密码',
        permiss: '12',
      },
    ]
  },
  {
    icon: 'CoffeeCup',
    index: '/journalMes',
    title: '日志信息',
    permiss: '7',
    subs:[
      {
        index: '/journal',
        title: '日志一览',
        permiss: '13',
      },
    ]
  },
];

const route = useRoute();
const onRoutes = computed(() => {
  return route.path;
});

const sidebar = useSidebarStore();
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
  width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}

.sidebar > ul {
  height: 100%;
}
</style>
